<form id="form">
<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal">
		<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
	</button>
	<small class="modal-title">
	<#if model??>
		<i class="fa fa-pencil"></i> 编辑	
	<#else>
		<i class="fa fa-plus"></i> 新增
	</#if>
	</small>
</div>
<div class="modal-body" style="padding:0px">
	<div class="tabs-container">
		<ul class="nav nav-tabs">
			<li class="active"><a data-toggle="tab" href="#tab-1"
				aria-expanded="false">基础信息</a></li>
			<li class=""><a data-toggle="tab" href="#tab-2"
				aria-expanded="true">自定义属性</a></li>
		</ul>
		<div class="tab-content" style="height:350px;overflow-y:auto;">
			<div id="tab-1" class="tab-pane active">
				<div class="panel-body" style="border-bottom:none">
					<div class="form-horizontal">
						<fieldset>
						<input type="hidden" name="id" value="<#if model??>${model.id!''}</#if>"/>
							<div class="form-group">
								<label class="col-sm-2 control-label">姓名：</label>
								<div class="col-sm-4">
									<input name="realname" type="text" placeholder="请输入姓名"
										class="form-control requiredIcon" value="<#if model??>${model.realname!''}</#if>">
								</div>
								<label class="col-sm-2 control-label">用户名：</label>
								<div class="col-sm-4">
									<input name="username" type="text" placeholder="请输入用户名"
										class="form-control requiredIcon" value="<#if model??>${model.username!''}</#if>">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">密码：</label>
								<div class="col-sm-4">
									<input name="password" type="password" placeholder="请输入密码"
										class="form-control requiredIcon" value="<#if model??>${model.password!''}</#if>">
								</div>
								<label class="col-sm-2 control-label">手机号码：</label>
								<div class="col-sm-4">
									<input name="mobile" type="text" placeholder="请输入手机号码" class="form-control" 
									value="<#if model??>${model.mobile!''}</#if>" maxlength="11">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">电子邮箱：</label>
								<div class="col-sm-4">
									<input name="email" type="email" placeholder="请输入电子邮箱" class="form-control"
									value="<#if model??>${model.email!''}</#if>">
								</div>
								<label class="col-sm-2 control-label">性别：</label>
								<div class="col-sm-4" style="height:34px">
									<div class="radio radio-inline radio-primary">
										<input type="radio" name="gender" id="gender1" value="true"
											<#if !model?? || (model?? && model.gender)>checked</#if> > <label for="gender1"> 男 </label>
									</div>
									<div class="radio radio-inline radio-danger">
										<input type="radio" name="gender" id="gender2" value="false"
										<#if model?? && !model.gender>checked</#if>>
										<label for="gender2"> 女 </label>
									</div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">选项：</label>
								<div class="col-sm-3" style="height:34px">
									<select name="type" class="form-control input-s-sm inline">
										<option value="1">管理员</option>
										<option value="2">业务员</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">选项：</label>
								<div class="col-sm-10" style="height:34px">
									<div class="checkbox checkbox-primary checkbox-inline">
                                        <input type="checkbox" name="enabled" id="enabled" <#if !model?? || (model?? && model.enabled)>checked</#if> value="true">
                                        <label for="enabled"> 启用 </label>
                                    </div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-2 control-label">备注：</label>
								<div class="col-sm-10">
									<textarea name="remark" rows="8" style="resize:vertical;" class="form-control" maxlength="200" placeholder="请输入备注信息"
									><#if model??>${model.remark!''}</#if></textarea>
								</div>
							</div>
						</fieldset>
					</div>
				</div>
			</div>
			<div id="tab-2" class="tab-pane">
				<div class="panel-body" style="border-bottom:none">
					<p>自定义属性</p>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="modal-footer">
	<button class="btn btn-white" data-dismiss="modal">关闭</button>
	<button class="btn btn-primary" type="submit">保存</button>
</div>
</form>
<script>
	$(function(){
		/*$(".department-select").chosen({
		   disable_search_threshold: 5,
		   no_results_text: "没有匹配的结果!",
		   width: "100%"
		});*/
        var validator = $("#form").validate({
      	  rules: {
                username: {
                    required: true,
                    remote: { //验证用户名是否存在
                       type: "POST",
                       url: "${basePath}/user/validateUsername",
                       dataType: "json",
                       data: {
                    	   id: $("#form input[name=id]").val()
                       },
                       dataFilter: function(data, type){
                    	   return data;
                       }
                    } 
                },
                realname: {
                    required: true
                },
                password: {
                    required: true
                },
                email: {
                    email: true
                },
                departmentId: {
                	required: true
                },
                mobile: {
                	mobile: true
                }
            },
            messages: {
                username: {
                    required: icon + "请输入用户名",
                    remote: icon + "用户名已存在"
                },
                realname: {
                    required: icon + "请输入姓名"
                },
                password: {
                    required: icon + "请输入密码"
                },
                email: {
                 	email: icon + "请输入正确的电子邮箱"
                },
                departmentId: {
                	required: icon + "请选择部门"
                },
                mobile: {
                	mobile: icon + "请输入正确的手机号码"
                }
            },
            submitHandler: function(form){
            	var id = $("#form input[name=id]").val();
            	var loading = layer.load();
            	$(form).ajaxSubmit({
               		url:"${basePath}/user/" + (id == "" ? "add" : "edit") ,
               	 	type:"POST",
               	 	dataType:"json",
               		data: {
            	 		enabled: $("#form input[name=enabled]").is(":checked")
            	 	},
               	 	success: function (resp) {
               	 		layer.close(loading);
               	 		$("#formModal").modal("hide");
	               	 	if(resp.code == 0) {
	                   		swal({
	                       		title: "操作成功",
	                       		type: "success",
	                       		showCancelButton: false,
	                       		confirmButtonColor: '#18a689',
	                       		confirmButtonText: '确定',
	                       		closeOnConfirm: true
	                         },
	                       	function(){
	                         	$("#table").bootstrapTable("refresh");
	                       	});
	               		}else if(resp.code == 1){
	               			swal("操作失败！", resp.data, "error");
	               		}else {
	               			swal("操作失败！", "未知错误", "error");
	               		}
                 	}
                });    
            },
            showErrors: function(errorMap, errorList) {
          		this.defaultShowErrors();
          }
        });
		
	})
</script>

